/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-03-31 10:45:52
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-03-21 17:57:55
 * @FilePath: /tea-garden-web/src/util/storeModules/manager-person.js
 * @Description:管理-人员
 */
import { get, post, patch, del } from '@/util/http.js'
import { DateTime } from 'luxon'
// import { userDetail } from '@/util/resData'
export default {
  state: {
    // 人员列表过滤值
    query: '',
    // 人员列表
    users: [],
    // 当前选中人员信息
    userInfo: '',
    // 人员农事
    userFarmWork: [],
    // 加载中
    loading: {
      users: false,
      userInfo: false,
      userFarmWork: false
    }
  },
  getters: {
    // 农事时间轴
    farmWorkTimeLine: (state) => {
      const data = {}
      // 从数据接口请求的userFarmWork数组, 已按照农事日期倒序排列
      // 如果Object的key为字符串则按照， v-for时的顺序和插入顺序保持一致
      state.userFarmWork.map((item) => {
        const date = DateTime.fromISO(item.planTime)
        if (!data[date.year + '年']) {
          data[date.year + '年'] = {}
        }
        if (!data[date.year + '年'][date.month + '月']) {
          data[date.year + '年'][date.month + '月'] = {}
        }
        if (!data[date.year + '年'][date.month + '月'][date.day + '日']) {
          data[date.year + '年'][date.month + '月'][date.day + '日'] = []
        }
        data[date.year + '年'][date.month + '月'][date.day + '日'].push(item)
        return item
      })
      return data
    }
  },
  mutations: {
    // 设置人员列表过滤值
    setQuery (state, payload) {
      state.query = payload.target.value
    },
    // 清除人员信息
    clearUserInfo (state) {
      state.userInfo = ''
    },
    // 清除人员农事
    clearUserFarmWork (state) {
      state.userFarmWork = []
    },
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    // 获取人员列表
    async getUsers ({ state, commit, dispatch }) {
      try {
        commit('setLoadingState', { key: 'users', value: true })
        commit('setCommonState', { key: 'users', value: [] })
        const { data } = await get({ url: `/userinfo/list?userName=${state.query}` })
        // const { data } = userList
        console.log(data)
        commit('setCommonState', { key: 'users', value: data })
        let id = ''
        if (state.userInfo.id) {
          id = state.userInfo.id
        } else if (state.users.length > 0) {
          id = state.users[0].id
        }
        dispatch('getUserDetail', id)
        dispatch('getUserFarmWork', id)
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'users', value: false })
    },
    // 获取人员详情
    async getUserDetail ({ state, commit }, payload) {
      try {
        commit('setLoadingState', { key: 'userInfo', value: true })
        commit('clearUserInfo')
        const data = await get({ url: `/userinfo/getDetailById/${payload}` })
        // const { data } = userDetail
        commit('setCommonState', { key: 'userInfo', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'userInfo', value: false })
    },
    // 获取人员农事记录
    async getUserFarmWork ({ state, commit }, payload) {
      try {
        commit('setLoadingState', { key: 'userFarmWork', value: true })
        commit('clearUserFarmWork')
        const { data } = await get({ url: `/userinfo/getFarmingById?id=${payload}` })
        state.userFarmWork = data
        commit('setCommonState', { key: 'userFarmWork', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'userFarmWork', value: false })
    },
    // 新增人员
    async addUser ({ dispatch }, payload) {
      try {
        await post({ url: '/userinfo/add', params: payload })
        dispatch('getUsers')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑人员
    async editUser ({ dispatch }, payload) {
      try {
        await patch({ url: '/userinfo/updateById', params: payload })
        dispatch('getUsers')
      } catch (error) {
        console.log(error)
      }
    },
    // 删除人员
    async deleteUser ({ dispatch, commit, state }, payload) {
      try {
        await del({ url: `/userinfo/removeByIds?ids=${payload}` })
        dispatch('getUsers')
        commit('clearUserInfo')
        commit('clearUserFarmWork')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
